package com.xudow.app.newui;
/**
 * 提问页面
 */
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.fragment.AskFragment;
import com.xudow.app.newui.fragment.QuestionsFragment;

import butterknife.BindView;

public class AskQuestionActivity extends BaseUiActivity implements View.OnClickListener {
    @BindView(R.id.ask_tv)
    TextView ask_tv;

    @BindView(R.id.questions_tv)
    TextView quesrions_tv;

    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;

    private AskFragment askFragment;


    private QuestionsFragment questionsFragment;


    FragmentManager fragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_question);
        fragmentManager=getSupportFragmentManager();
        setTabSelection(0);
    }

    @Override
    protected void initData() {

    }

    public void onBackBtnClick(View view){
        finish();
    }
    @Override
    protected void initListener() {
        ask_tv.setOnClickListener(this);
        quesrions_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {


        switch (v.getId()){
            case R.id.ask_tv:
              iv1.setVisibility(View.VISIBLE);
                ask_tv.setTextColor(Color.parseColor("#55c11f"));
              iv2.setVisibility(View.GONE);
                quesrions_tv.setTextColor(Color.BLACK);

                setTabSelection(0);

//                if (null==askFragment)
//                    askFragment=new AskFragment();
//
//                fragmentTransaction.add(R.id.fregmentgroup,askFragment);
//                if (null!=questionsFragment)
//                    fragmentTransaction.remove(questionsFragment);
                break;
            case R.id.questions_tv:
                iv2.setVisibility(View.VISIBLE);
                quesrions_tv.setTextColor(Color.parseColor("#55c11f"));
                iv1.setVisibility(View.GONE);
                ask_tv.setTextColor(Color.BLACK);

                setTabSelection(1);

//                if (null==questionsFragment)
//                    questionsFragment=new QuestionsFragment();
//                fragmentTransaction.add(R.id.fregmentgroup,questionsFragment);
//                if (null!=askFragment)
//                    fragmentTransaction.remove(askFragment);
                break;
        }


    }



    /**
     * Fragment进行替换，注意替换前把所有的fragment先隐藏，否则会发生多个framgent显示在界面上
     * @param index
     */
    public void setTabSelection(int index) {
        //开启一个framgent事物
        FragmentTransaction transaction=fragmentManager.beginTransaction();

        hideFragments(transaction);
        switch (index){
            case 0:

                if(askFragment==null){
                    askFragment=new AskFragment();
                    transaction.add(R.id.fregmentgroup,askFragment);
                }else{
                    transaction.show(askFragment);
                }

                break;
            case 1:

                if(questionsFragment==null){
                    questionsFragment=new QuestionsFragment();
                    transaction.add(R.id.fregmentgroup,questionsFragment);
                }else{
                    transaction.show(questionsFragment);
                }
                break;

            default:
                break;
        }
        transaction.commit();
    }


    /**
     *    先隐藏所有的fragment，已防多个framgnet显示在界面上
     */
    private void hideFragments(FragmentTransaction transaction) {

        if(askFragment!=null){
            transaction.hide(askFragment);
        }
        if(questionsFragment!=null){
            transaction.hide(questionsFragment);
        }

    }


}
